package inet.app.vod.db;

import inet.app.vod.object.CategoryModel;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.inet.app.framework.util.Log;

public class DaoCategory extends DaoBase {

	public DaoCategory(Context context) {
		super(context);
	}

	private ContentValues createContentValueFromRow(CategoryModel row) {

		ContentValues value = new ContentValues();
		value.put(DBConfig.CATEGORY_ID, row.getId());
		value.put(DBConfig.CATEGORY_REMOTE_ID, row.getRemoteId());
		value.put(DBConfig.CATEGORY_NAME, row.getName());
		value.put(DBConfig.CATEGORY_GROUP_ID, row.getGroupId());
		value.put(DBConfig.CATEGORY_IS_GROUP, row.hasGroup());
		return value;
	}

	private CategoryModel createRowFromCursor(Cursor cursor) {
		CategoryModel row = new CategoryModel();
		row.setId(cursor.getString(cursor.getColumnIndex(DBConfig.CATEGORY_ID)));
		row.setRemoteId((cursor.getString(cursor
				.getColumnIndex(DBConfig.CATEGORY_REMOTE_ID))));
		row.setName(cursor.getString(cursor
				.getColumnIndex(DBConfig.CATEGORY_NAME)));
		row.setGroupId(cursor.getString(cursor
				.getColumnIndex(DBConfig.CATEGORY_GROUP_ID)));
		row.setGroup((cursor.getInt(cursor
				.getColumnIndex(DBConfig.CATEGORY_IS_GROUP))));
		return row;
	}

	public long insertRow(CategoryModel row) {
		ContentValues value = createContentValueFromRow(row);
		return insertRow(DBConfig.TABLE_CATEGORY, value);
	}

	public long insertCollection(List<CategoryModel> rows) {
		List<ContentValues> values = new ArrayList<ContentValues>();
		for (CategoryModel row : rows) {
			ContentValues value = createContentValueFromRow(row);
			values.add(value);
		}
		return insertCollection(DBConfig.TABLE_CATEGORY, values);
	}

	public int deleteAll() {
		return deleteAll(DBConfig.TABLE_CATEGORY);
	}

	public int deleteRow(String id) {
		int res = -1;
		if (!dbConnection.openSession())
			return res;
		Log.d(tag, "deleting row");
		Cursor cursor = dbConnection.query(DBConfig.TABLE_CATEGORY, null,
				DBConfig.CATEGORY_ID + "=?", new String[] { id }, null, null,
				null);

		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then delete row */
				res = dbConnection.delete(DBConfig.TABLE_CATEGORY,
						DBConfig.CATEGORY_ID + "=?", new String[] { id });
			} else {
				/** row is not existed */
				res = 0;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	/**
	 * to update a row
	 * 
	 * @param row
	 * @return
	 */
	public int updateRow(CategoryModel row) {
		int res = -1;
		CategoryModel old = selectRow(row.getId());
		if (old == null)
			return res;
		Log.d("updating row");

		Cursor cursor = dbConnection.query(DBConfig.TABLE_CATEGORY, null,
				DBConfig.CATEGORY_ID + "=?", new String[] { row.getId() },
				null, null, null);
		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then update row */
				ContentValues value = createContentValueFromRow(row);
				res = dbConnection.update(DBConfig.TABLE_CATEGORY, value,
						DBConfig.CATEGORY_ID + "=?",
						new String[] { row.getId() });
			} else {
				/** row is not existed */
				res = -1;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	public CategoryModel selectRow(String id) {
		CategoryModel row = null;
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return row;
			Log.d("selecting row");
			cursor = dbConnection.query(DBConfig.TABLE_CATEGORY, null,
					DBConfig.CATEGORY_ID + "=?", new String[] { id }, null,
					null, null);

			if (cursor != null) {
				int rows = cursor.getCount();
				if (rows > 0) {
					/** row is existed, then get row info */
					cursor.moveToFirst();
					row = createRowFromCursor(cursor);
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			// String test = "1";
			dbConnection.closeSession();
		}
		return row;
	}

	public List<CategoryModel> selectCategoryFromGroup(String groupId) {
		List<CategoryModel> rows = new ArrayList<CategoryModel>();
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return rows;
			cursor = dbConnection.query(DBConfig.TABLE_CATEGORY, null,
					DBConfig.CATEGORY_GROUP_ID + "=?",
					new String[] { groupId }, null, null, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						CategoryModel row = createRowFromCursor(cursor);
						rows.add(row);

					} while (cursor.moveToNext());
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return rows;
	}

	public List<CategoryModel> selectAll() {

		List<CategoryModel> rows = new ArrayList<CategoryModel>();
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return rows;
			cursor = dbConnection.query(DBConfig.TABLE_CATEGORY, null, null,
					null, null, null, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						CategoryModel row = createRowFromCursor(cursor);
						rows.add(row);

					} while (cursor.moveToNext());
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return rows;
	}

	public boolean isExistData() {
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return false;
			cursor = dbConnection.query(DBConfig.TABLE_CATEGORY, null, null,
					null, null, null, null);
			if (cursor != null && cursor.moveToFirst()) {
				return true;
			}
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return false;
	}
}
